const codeidea = require("../models/codeidea")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else { 
            if(fields.id!==""){
                codeidea.findByIdAndUpdate(fields.id, {
                    UUID: fields.UUID,
                    pointId: fields.pointId,
                    demoId: fields.demoId,
                    process: fields.process
                }, (error, doc) => {
                    // console.log(doc)
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({
                            'state': true,
                            'msg': '更新思路成功！' 
                        })
                    }
                })
            }else{
                codeidea.create({
                    UUID: fields.UUID,
                    pointId: fields.pointId,
                    demoId: fields.demoId,
                    process: fields.process
                }, (error, doc) => {
                    // console.log(doc)
                    if (error) {
                        res.json({ 'state': false, 'msg': error });
                    } else {
                        res.json({
                            'state': true,
                            'msg': '保存思路成功！' 
                        })
                    }
                })
            }
        }
    });

});

module.exports = router;